﻿using AiChiTu.TraceabilitySystem.Domain.AiChiTuManagement;
using AiChiTu.TraceabilitySystem.ErrorCode;
using AiChiTu.TraceabilitySystem.Infrastructure.Interfaces;
using AiChiTu.TraceabilitySystem.Read.API.Applications.Commands.Productbasicses;
using AiChiTu.TraceabilitySystem.Read.API.Dtos.Productbasicses;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AiChiTu.TraceabilitySystem.Read.API.Applications.CommandsHandlers.Productbasicses
{
	public class QueryProductbasicDetailsByIdCommandHandle : IRequestHandler<QueryProductbasicDetailsByIdCommand, APIResult<QueryProductbasicDetailsByIdDTO>>
	{
		private readonly IBaseRepository<ProductBasics> productbasicRepository;
		private readonly IBaseRepository<ProductName> productnameRepository;
		private readonly IBaseRepository<Breeding> breedingRepository;
		private readonly IBaseRepository<HealthInfo> healthRepository;
		private readonly IBaseRepository<ImagePicture> imagepictureRepository;
		private readonly IBaseRepository<BreedingCompany> breedingcompanyRepository;
		private readonly IBaseRepository<Slaughter> slaughterRepository;
		private readonly IBaseRepository<SlaughterCompany> slaughtercompanyRepository;
		private readonly IBaseRepository<DeepProcessing> deepprocessRepository;
		private readonly IBaseRepository<DeepProcessingCompany> deepprocecompanyRepository;
		private readonly IBaseRepository<Supplier> supplierRepository;

		public QueryProductbasicDetailsByIdCommandHandle(IBaseRepository<ProductBasics> productbasicRepository,IBaseRepository<ProductName> productnameRepository, IBaseRepository<Breeding> breedingRepository, IBaseRepository<HealthInfo> healthRepository, IBaseRepository<ImagePicture> imagepictureRepository, IBaseRepository<BreedingCompany> breedingcompanyRepository, IBaseRepository<Slaughter> slaughterRepository, IBaseRepository<SlaughterCompany> slaughtercompanyRepository, IBaseRepository<DeepProcessing> deepprocessRepository, IBaseRepository<DeepProcessingCompany> deepprocecompanyRepository, IBaseRepository<Supplier> supplierRepository)
		{
			this.productbasicRepository = productbasicRepository;
			this.productnameRepository = productnameRepository;
			this.breedingRepository = breedingRepository;
			this.healthRepository = healthRepository;
			this.imagepictureRepository = imagepictureRepository;
			this.breedingcompanyRepository = breedingcompanyRepository;
			this.slaughterRepository = slaughterRepository;
			this.slaughtercompanyRepository = slaughtercompanyRepository;
			this.deepprocessRepository = deepprocessRepository;
			this.deepprocecompanyRepository = deepprocecompanyRepository;
			this.supplierRepository = supplierRepository;
		}
		/// <summary>
		/// 查询商品详情信息请求命令处理
		/// </summary>
		/// <param name="request">查询商品详情信息请求命令</param>
		/// <param name="cancellationToken">取消</param>
		/// <returns>返回任务</returns>
		public async Task<APIResult<QueryProductbasicDetailsByIdDTO>> Handle(QueryProductbasicDetailsByIdCommand request, CancellationToken cancellationToken)
		{
			APIResult<QueryProductbasicDetailsByIdDTO> result = APIResult<QueryProductbasicDetailsByIdDTO>.Result(ResultCode.QuerySuccess);

			IQueryable<QueryProductbasicDetailsByIdDTO> query = from pb in productbasicRepository.QueryAllAsync()
																join pn in productnameRepository.QueryAllAsync() on pb.ProductNameId equals pn.ProductNameId
																join b in breedingRepository.QueryAllAsync() on pb.ProductBasicsId equals b.ProductBasicsId
																join hl in healthRepository.QueryAllAsync() on pb.ProductBasicsId equals hl.ProductBasicsId
																join ip in imagepictureRepository.QueryAllAsync() on pb.ProductBasicsId equals ip.ProductBasicsId
																join bc in breedingcompanyRepository.QueryAllAsync() on pb.ProductBasicsId equals bc.ProductBasicsId
																join sl in slaughterRepository.QueryAllAsync() on pb.ProductBasicsId equals sl.ProductBasicsId
																join slc in slaughtercompanyRepository.QueryAllAsync() on pb.ProductBasicsId equals slc.ProductBasicsId
																join dp in deepprocessRepository.QueryAllAsync() on pb.ProductBasicsId equals dp.ProductBasicsId
																join sup in supplierRepository.QueryAllAsync() on pb.ProductBasicsId equals sup.ProductBasicsId
																join dpc in deepprocecompanyRepository.QueryAllAsync() on pb.ProductBasicsId equals dpc.ProductBasicsId
																where pb.ProductBasicsId == request.ProductBasicsId
																select new QueryProductbasicDetailsByIdDTO
																{
																	ProductBasicsDTO = new ProductBasicsDto()
																	{
																		ProductBasicsId = pb.ProductBasicsId,
																		ProductNameId = pb.ProductNameId,
																		ProductNamestring = pn.ProductNamestring,
																		Productionbatch = pb.Productionbatch,
																		Expirationdate = pb.Expirationdate,
																		ProductionLicenseNumber = pb.ProductionLicenseNumber,
																		ProductionCompany = pb.ProductionCompany,
																		ProductPicture = pb.ProductPicture,
																	},
																	BreedingDTO = new Breeding()
																	{
																		BreedingId = b.BreedingId,
																		BreedingType = b.BreedingType,
																		Source = b.Source,
																		FeedingDays = b.FeedingDays,
																		FeedBrand = b.FeedBrand,
																		Principal = b.Principal,
																		PublicationDate = b.PublicationDate,
																	},
																	HealthInfoDTOs = (from aa in healthRepository.QueryAllAsync()
																					  where aa.ProductBasicsId == pb.ProductBasicsId
																					  select new HealthInfo()
																					  {
																						  HealthInfoId = aa.HealthInfoId,
																						  FeedingDays = aa.FeedingDays,
																						  HealthType = aa.HealthType,
																						  HealthMethod = aa.HealthMethod,
																					  }).ToList(),
																	ImagePictureDTOs = (from ai in imagepictureRepository.QueryAllAsync()
																						where ai.ProductBasicsId == pb.ProductBasicsId
																						select new ImagePicture()
																						{
																							ImagePictureId = ai.ImagePictureId,
																							WorkFlow = ai.WorkFlow,
																							WorkFlowName = ai.WorkFlowName,
																							ImageType = ai.ImageType,
																							ImageTypeName = ai.ImageTypeName,
																							ImageURL = ai.ImageURL,
																						}).ToList(),
																	BreedingCompanyDTO = new BreedingCompany()
																	{
																		BreedingCompanyId = bc.BreedingCompanyId,
																		BreedingCompanyName = bc.BreedingCompanyName,
																		Principal = bc.Principal,
																		BreedingNum = bc.BreedingNum,
																	},
																	SlaughterDTO = new Slaughter()
																	{
																		SlaughterId = sl.SlaughterId,
																		SlaughterBatch = sl.SlaughterBatch,
																		WeightFactory = sl.WeightFactory,
																		ArrivalTtime = sl.ArrivalTtime,
																		SlaughterTime = sl.SlaughterTime,
																	},
																	SlaughterCompanyDTO = new SlaughterCompany()
																	{
																		SlaughterCompanyId = slc.SlaughterCompanyId,
																		SlaughterCompanyName = slc.SlaughterCompanyName,
																		CreateDate = slc.CreateDate,
																		Province = slc.Province,
																		City = slc.City,
																		County = slc.County,
																		CompanyProfile = slc.CompanyProfile,
																	},
																	DeepProcessingDTO = new DeepProcessing()
																	{
																		DeepProcessingId = dp.DeepProcessingId,
																		TeamGroup = dp.TeamGroup,
																		Principal = dp.Principal,
																	},
																	SupplierDTOs = (from asup in supplierRepository.QueryAllAsync()
																					where asup.ProductBasicsId == pb.ProductBasicsId
																					select new Supplier()
																					{
																						SupplierId = asup.SupplierId,
																						MaterialsType = asup.MaterialsType,
																						ProductionBatchNo = asup.ProductionBatchNo,
																						ImageURL = asup.ImageURL,
																					}).ToList(),
																	DeepProcessingCompanyDTO = new DeepProcessingCompany()
																	{
																		DeepProcessingCompanyId = dpc.DeepProcessingCompanyId,
																		DeepProcessingCompanyName = dpc.DeepProcessingCompanyName,
																		ProductionLicenseNo = dpc.ProductionLicenseNo,
																	},

																};
			result = APIResult<QueryProductbasicDetailsByIdDTO>.Result(ResultCode.QuerySuccess, await query.FirstOrDefaultAsync());

			return await Task.FromResult(result);
		}
	}
}
